package com.game.server;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class ConfigManager {
	
	Logger log = Logger.getLogger(this.getClass().getName());

	private static ConfigManager _instance;

	private ConfigManager() throws Exception 
	{
	}
	
	public static synchronized ConfigManager getInstance() throws Exception {
		if (_instance == null) {
			_instance = new ConfigManager();
		}
		return _instance;
	}
	private List<ServerConfig> server;
	
	private void init(String path)
	{
	    server = new ArrayList<ServerConfig>();
		try {
		    SAXBuilder builder = new SAXBuilder();  
		    //Document doc = builder.build(new File(System.getProperty("user.dir") + System.getProperty("file.separator")+path));
		    Document doc = builder.build(new File(path));
		    Element foo = doc.getRootElement();  
		    List<Element> allChildren = foo.getChildren();  
		    for(int i=0;i<allChildren.size();i++) 
		    {  
		    	server.add(new ServerConfig(allChildren.get(i)));
		    }
		}
		catch(Exception ex)
		{
			log.error("文件地址："+ path +"错误消息:"+ex.getMessage());
			ex.printStackTrace();
		}
	}

	

	public List<ServerConfig> getServer(String path) {
		init(path);
		return server;
	}
}
